#!/bin/sh

PREREQ=""

prereqs()
{
	echo "$PREREQ"
}

case $1 in
	prereqs)
		prereqs
		exit 0
		;;
esac


TTY_NUM=8
TTY_DEVICE="/dev/tty$TTY_NUM"

PUBKEY_TIME=1427251894
CURRENT_TIME=$(date +%s)

COMMON_MSG_ZH="因授权问题，系统上次自动关机。"
COMMON_MSG_EN="Because of Activation problem, system was automatically shut down last time."

ply_show_message() {
	plymouth message --text="$1"
	plymouth message --text="keys:请按S跳过此消息以继续"
	plymouth watch-keystroke --keys="Ss"

	plymouth message --text=""
	plymouth message --text="keys:"
}

TMP_GETCH=

getch() {
	TTY_SETTINGS=$(stty $STTY_DEVICE -g)
	stty $STTY_DEVICE raw -echo
	TMP_GETCH=$(dd bs=1 count=1 2> /dev/null)
	STAT_GETCH="$?"
	stty $STTY_DEVICE "$TTY_SETTINGS"
	
	return ${STAT_GETCH}
}

text_show_message_real() {
	clear

	echo "$1"

	echo "Please enter \"S\" or \"s\" to skip: "

	RET_VALUE=1
	while [ $RET_VALUE != 0 ]; do
		getch
		echo "$TMP_GETCH" | grep -q '[sS]'
		RET_VALUE=$?
	done

	clear
}

text_show_message() {
	plymouth --ping && plymouth quit || echo ""

	for x in $(cat /proc/cmdline); do
		echo "$x" | grep -q '^console=' && CONSOLE_IS_SET=y && break \
			|| CONSOLE_IS_SET=n
	done

	if [ "$CONSOLE_IS_SET" != "y" ]; then
		text_show_message_real "$1"
	else
		STTY_DEVICE="-F $TTY_DEVICE"
		chvt "$TTY_NUM"
		text_show_message_real "$1" < $TTY_DEVICE > $TTY_DEVICE
	fi

	sleep 1
	printf '\033[?25l' > /dev/tty7

	if [ ! -d /run/plymouth ]; then
		mkdir /run/plymouth
	fi

	/sbin/plymouthd --mode=boot --attach-to-session --pid-file=/run/plymouth/pid
	/bin/plymouth show-splash
}

show_message() {
#	plymouth --ping && PLY_PLUGIN_PATH=$(plymouth --get-splash-plugin-path) && [ -f "$PLY_PLUGIN_PATH/label.so" ] && ply_show_message "$1" || text_show_message "$2"
	text_show_message "$2"
}

# first of all, some needed checks

# check for LICENSE and kyinfo file
if [ ! -e ${rootmnt}/etc/LICENSE ] || [ ! -e ${rootmnt}/etc/.kyinfo ]; then
	show_message '该系统可能不是一个有效的麒麟操作系统：无授权文件' 'This system may be not a valid Kylin operation system: no authorization files'
	exit 0
fi

# check for kylin_activation_check
if [ ! -e ${rootmnt}/usr/bin/kylin_activation_check ]; then
	show_message '该系统可能不是一个有效的麒麟操作系统：无激活检查程序' 'This system may be not a valid Kylin operation system: no activation check program'
	exit 0
fi

# check for system time
if [ $CURRENT_TIME -le $PUBKEY_TIME ]; then
	show_message '系统时间异常' 'There is something wrong with system time'
	exit 0
fi


# then, check log file
handle_log_legacy() {
	grep -v '^$' ${rootmnt}/var/log/kylin-activation-check | grep 'Time' | tail -n 2 | grep -q '系统在试用期内' && return || \
	show_message "$COMMON_MSG_ZH" "$COMMON_MSG_EN"
}

handle_log_with_type() {
	echo "$1" | grep -q '^(WW)' && show_message "系统激活已到期" "System activation has expired" && return
	echo "$1" | grep -q '^(II)' && return || show_message "$COMMON_MSG_ZH" "$COMMON_MSG_EN"
}

handle_log() {
	echo "$1" | grep -q -E '系统已激活|系统已经激活' && return
	echo "$1" | grep -q '^([^)]\{2,2\})' && handle_log_with_type "$1" || handle_log_legacy "$1"
}

if [ ! -e ${rootmnt}/var/log/kylin-activation-check ]; then
	exit 0
fi

grep -q 'Time' ${rootmnt}/var/log/kylin-activation-check || exit 0

LOG_LINE=$(grep -v '^$' ${rootmnt}/var/log/kylin-activation-check | grep 'Time' | tail -n 1)

handle_log "$LOG_LINE"

